<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        table{
            border: 1px solid #e9e9e9;
            border-collapse: collapse;
            border-spacing: 0;
        }
        th,td{
            padding: 8px 16px;
            border: 1px solid #e9e9e9;
            text-align: left;
        }
        th{
            background-color: #f7f7f7;
            color: #5c6b77;
            font-weight: 600;
        }
    </style>
</head>
<body>
    <!-- 购物车备课敲写版 -->
    <div id="app">
        <div v-if="books.length > 0">
            <table>
                <thead>
                    <tr>
                        <th></th>
                        <th>书籍名称</th>
                        <th>出版日期</th>
                        <th>价格</th>
                        <th>购买数量</th>
                        <th>操作</th>
                    </tr>
                </thead>
                <tbody>
                    <tr v-for="(item,index) in books">
                        <td>{{ item.id }}</td>
                        <td>{{ item.name }}</td>
                        <td>{{ item.time }}</td>
                        <td>{{ item.price | showPrice }}</td>
                        <td>
                            <button @click="reduce(index)" :disabled="item.count <=1 ">-</button>
                            {{ item.count}}
                            <button @click="add(index)">+</button>
                        </td>
                        <td>
                            <button @click="deleteData(index)">移除</button>
                        </td>
                    </tr>
                </tbody>
            </table>
            <div>总价格{{ allPrice }}</div>
        </div>
        <div v-else>购物车为空</div>
    </div>
    <script src="../js/vue.js"></script>
    <script>
        const app = new Vue({
            el:'#app',
            data(){
                return {
                    books:[
                        {   
                            id:1,
                            name:'西游记',
                            time:'2020-5-24',
                            price:85,
                            count:1
                        },
                        {
                            id:2,
                            name:'红楼梦',
                            time:'2020-5-24',
                            price:88,
                            count:1
                        },
                        {
                            id:3,
                            name:'三国演义',
                            time:'2020-5-24',
                            price:87,
                            count:1
                        },
                        {
                            id:4,
                            name:'水浒传',
                            time:'2020-5-24',
                            price:89,
                            count:1
                        },
                    ]
                }
            },
            methods:{
                reduce(i){
                    if(this.books[i].count > 1){
                        this.books[i].count--
                    }
                },
                add(i){
                    this.books[i].count++
                },
                deleteData(i){
                    this.books.splice(i,1)
                }
            },
            computed:{
                allPrice(){
                    let totalPrice = 0
                    for(let i = 0 ; i < this.books.length ; i++){
                        totalPrice += this.books[i].price * this.books[i].count
                    }
                    return totalPrice
                }
            },
            filters:{
                showPrice(val){
                    return '¥' + val.toFixed(2)
                }
            }
        })
    </script>
</body>
</html>